'use strict';

const { Controller } = require('ee-core');
const Log = require('ee-core/log');
const Services = require('ee-core/services');
const Addon = require('ee-core/addon');
const axios = require('axios');

/**
 * wmx
 * @class
 */
class WmxController extends Controller {

  constructor(ctx) {
    super(ctx);
  }


  /**
   * 所有方法接收两个参数
   * @param args 前端传的参数
   * @param event - ipc通信时才有值。详情见：控制器文档
   */

  async ipcSendMsg(args, event) {
    const { type, content } = args;
    const data = await Services.get('framework').bothWayMessage(type, content, event);
    console.log("wmx ipcSendMsg")
    console.log(JSON.stringify(data))
    return data;
  }
 
  async sellerProfileAjax() {

    return new Promise(async function (resolve, reject) {
        const data = await Services.get('wmx').sellerProfileAjax();

        resolve(data);
    })
  }
 
  async arfq(args) {


       
  
      return "";

      
  }

  /**
   * test
   */
  out () {
    Services.get('storage').delTestDataSqlite('cookie')
    Services.get('storage').delTestDataSqlite('token_csrf')
    Services.get('storage').delTestDataSqlite('xSRFTOKEN')
    Services.get('storage').delTestDataSqlite('tb_token')
    Services.get('storage').delTestDataSqlite('ctoken')
  }

  
  marketing () {

      return new Promise(async function (resolve, reject) {
        const data = await Services.get('wmx').marketing();
        console.log(JSON.stringify(data.data.overview))
        resolve(data.data.overview);
    })
  
  }

  test () {

   

        return new Promise(async function (resolve, reject) {

          var alicookie =await  Services.get('storage').getTestDataSqlite('cookie');
          var ctoken =await  Services.get('storage').getTestDataSqlite('ctoken');
          var _tb_token_ =await  Services.get('storage').getTestDataSqlite('tb_token');
    
       

            try {
              var ctoken=ctoken.cookie;
            } catch (error) {
              return  resolve(false);
            }

            let config = {
              method: 'get',
              maxBodyLength: Infinity,
              url: 'https://www2.alibaba.com/api/csrf?ctoken='+ctoken.cookie+'&_tb_token_='+_tb_token_.cookie,
              headers: { 
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36:', 
                'Cookie':alicookie.cookie
              }
            };

            axios.request(config).then((response) => {

              var token_csrf=response.data.token
              console.log(token_csrf)
              if(token_csrf){
                console.log("true")
                resolve(true);
              }else{
                  Services.get('storage').delTestDataSqlite('cookie')
                  Services.get('storage').delTestDataSqlite('token_csrf')
                  Services.get('storage').delTestDataSqlite('xSRFTOKEN')
                  Services.get('storage').delTestDataSqlite('tb_token')
                  Services.get('storage').delTestDataSqlite('ctoken')

                  console.log("false 3")

                  resolve(false);
              }
          })
          .catch((error) => {
              console.log("false 3")
              resolve(false);
          });
      })


  }

 
  
  /**
   * test
   */
  async testService () {
    const serviceResult2 = await Services.get('example').test('electron');
    Log.info('service result2:', serviceResult2);

    return;
  }
  
  /**
   * test
   */
  async testAddon () {
    const trayResult2 = Addon.get('tray').hello();
    Log.info('addon result2:', trayResult2);

    return;
  } 

}

WmxController.toString = () => '[class WmxController]';
module.exports = WmxController;  